from requests.auth import HTTPBasicAuth
import requests
import threading
from datetime import datetime

# "http://localhost:8000/api/User/getToken/",
host="https://test.admin.tsl-ewo.gnshk.com/"
host="https://efmiddlewaredev.tslj.com.cn/middleware/api/"
FGR_TIMEOUT=60

def login():
    url = host+"User/getToken/"
    req = requests.post(
                url,
                json={
                    "username": "FGRAPI",
                    "password": "123456",
                },
                headers={'Content-Type':'application/json'}
            )

    print(req.json())
    res_json = req.json()

    if res_json["status"]:
        token='Bearer '+res_json["data"]
    else:
        token=''

    return token


def pushOrder(token):


    print(token)

    orders = []

    for i in range(100):
        orderNo = str(50000+i)
        order ={
            "Article": "200001BC008HH08X00",
            "order": orderNo,
            "due_date": "2023-06-30T00:00:00",
            "updated_due_date": "2023-06-29T00:00:00",
            "quantity": 2,
            "goods_type": "PT950",
            "description": "",
            "ring_type": "H16X",
            "gold_type": "FG-RI",
            "site": "",
            "ready_date": "2023/5/31 0:00:00",
            "pos": "CM快線&凈托",
            "design": "BA665",
            "mould": 1,
            "black_pic_url": "http://172.16.3.14/image/BA665.jpg",
            "remark": "WH",
            "effect": "收货后转CM快线仓;用H17改蠟;凹位噴沙",
            "product_mark_content": "WH",
            "product_mark_description": "",
            "product_mark_font": "",
            "ProdStandardBOMs": [
                {
                    "article": "DDDDFCXXEEP0000XZ",
                    "article_description": "圆钻石 EE级 (0000PL)",
                    "qty": 2,
                    "weight": 1.006,
                    "unit": "CT",
                    "stone_type": "2",
                    "is_stone": True,
                    "is_main_stone": False
                },
                {
                    "article": "GCC750Y00",
                    "article_description": "18K黄金",
                    "qty": 0,
                    "weight": 1.425,
                    "unit": "G",
                    "stone_type": "P",
                    "is_stone": False,
                    "is_main_stone": False
                },
                {
                    "article": "GDD750W00",
                    "article_description": "18K白 (PY用)",
                    "qty": 0,
                    "weight": 1.425,
                    "unit": "G",
                    "stone_type": "P",
                    "is_stone": False,
                    "is_main_stone": False
                },
                {
                    "article": "MMSP00031X",
                    "article_description": "圆形月光石 (1MM)",
                    "qty": 1,
                    "weight": 0.004,
                    "unit": "CT",
                    "stone_type": "2",
                    "is_stone": True,
                    "is_main_stone": False
                },
                {
                    "article": "DDDDFCXXFFP0XXXX",
                    "article_description": "圆钻石 FF级 (0PL)",
                    "qty": 110,
                    "weight": 0.774,
                    "unit": "CT",
                    "stone_type": "2",
                    "is_stone": True,
                    "is_main_stone": False
                },
                {
                    "article": "GCC750R00",
                    "article_description": "18K红金",
                    "qty": 1,
                    "weight": 0.004,
                    "unit": "CT",
                    "stone_type": "2",
                    "is_stone": True,
                    "is_main_stone": False
                },
                {
                    "article": "MSAQ04100X",
                    "article_description": "圆形蓝宝石 (00PL)",
                    "qty": 1,
                    "weight": 0.004,
                    "unit": "CT",
                    "stone_type": "2",
                    "is_stone": True,
                    "is_main_stone": False
                }
            ]
        }

        orders.append(order)


    print(f"Send {len(orders)} orders to EWO ")

    data = {"orders": orders}

    startTime = datetime.now()

    url = host+"api/queryProdOrder/"
    req = requests.post(
        url,
        json=data,
        headers={'Content-Type':'application/json', 'Authorization':token}
    )

    endTime = datetime.now()

    print(f"process time: {endTime-startTime}")

    res=req.json()
    print(req.json())

def queryStoneImgs(token, order):
    url = "ewo_prodorder/queryStoneImgs"
    data = {"orderNo": order}
    print(f"Send request to {url}: {data}")
    # headers={'Content-Type':'application/x-www-form-urlencoded', 'authorization':token}
    headers={'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzMzg1IiwiaWF0IjoiMTY5ODkyNTM3OCIsIm5iZiI6IjE2OTg5MjUzNzgiLCJleHAiOiIxNjk4OTMyNTc4IiwiaXNzIjoidm9sLmNvcmUub3duZXIiLCJhdWQiOiJ2b2wuY29yZSJ9.RuTIUrTSKT8aEunbKVlI6pIR4N2aidwqLKd0cjIWqL8', 'Content-Type': 'application/x-www-form-urlencoded'}
    # response = requests.post(
    #     host + url,
    #     data=data,
    #     headers=headers
    # )
    method="post"
    # response = requests.request(method, host + url, data=data, headers=headers, timeout=FGR_TIMEOUT)
    # response = requests.post(host + url, data=data)
    response = requests.request(method, host + url, json=data, headers=headers, timeout=FGR_TIMEOUT)
    print(f"{response=}")
    json_response = response.json()
    print(f"{json_response=}")
            
    
if __name__ == '__main__':
    token = login()
    queryStoneImgs(token, "100000365053")

    # pushOrder(token)

    # Create a list of orders
    orders = [1, 2, 3]

    # Create and start threads for each order
    # threads = []
    # for order in orders:
    #     thread = threading.Thread(target=pushOrder, args=(token,))
    #     thread.start()
    #     threads.append(thread)

    # # Wait for all threads to complete
    # for thread in threads:
    #     thread.join()

    # print("All orders pushed")




